import swaggerJSDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';
import { Express } from 'express';

const options: swaggerJSDoc.Options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: '小米有品商城API',
      version: '1.0.0',
      description: '基于Node.js + Express + TypeScript开发的小米有品商城后端API服务',
      contact: {
        name: 'API支持',
        email: 'support@xiaomiyoupin.com'
      },
      license: {
        name: 'MIT',
        url: 'https://opensource.org/licenses/MIT'
      }
    },
    servers: [
      {
        url: 'http://localhost:3000',
        description: '开发服务器'
      },
      {
        url: 'https://api.xiaomiyoupin.com',
        description: '生产服务器'
      }
    ],
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT'
        }
      },
      schemas: {
        User: {
          type: 'object',
          properties: {
            id: { type: 'integer', description: '用户ID' },
            username: { type: 'string', description: '用户名' },
            email: { type: 'string', format: 'email', description: '邮箱地址' },
            role: { type: 'string', enum: ['admin', 'user'], description: '用户角色' },
            avatar: { type: 'string', description: '头像URL' },
            phone: { type: 'string', description: '手机号' },
            status: { type: 'string', enum: ['active', 'inactive'], description: '用户状态' },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
          }
        },
        Product: {
          type: 'object',
          properties: {
            id: { type: 'integer', description: '商品ID' },
            name: { type: 'string', description: '商品名称' },
            description: { type: 'string', description: '商品描述' },
            price: { type: 'number', format: 'float', description: '商品价格' },
            originalPrice: { type: 'number', format: 'float', description: '原价' },
            category: { type: 'string', description: '商品分类' },
            brand: { type: 'string', description: '品牌' },
            images: { type: 'array', items: { type: 'string' }, description: '商品图片' },
            stock: { type: 'integer', description: '库存数量' },
            status: { type: 'string', enum: ['active', 'inactive'], description: '商品状态' },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
          }
        },
        Order: {
          type: 'object',
          properties: {
            id: { type: 'integer', description: '订单ID' },
            userId: { type: 'integer', description: '用户ID' },
            orderNumber: { type: 'string', description: '订单号' },
            totalAmount: { type: 'number', format: 'float', description: '订单总金额' },
            status: { type: 'string', enum: ['pending', 'paid', 'shipped', 'delivered', 'cancelled'], description: '订单状态' },
            shippingAddress: { type: 'string', description: '收货地址' },
            paymentMethod: { type: 'string', description: '支付方式' },
            notes: { type: 'string', description: '订单备注' },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
          }
        },
        Banner: {
          type: 'object',
          properties: {
            id: { type: 'integer', description: '轮播图ID' },
            title: { type: 'string', description: '轮播图标题' },
            image: { type: 'string', description: '轮播图图片URL' },
            linkUrl: { type: 'string', description: '跳转链接' },
            sortOrder: { type: 'integer', description: '排序顺序' },
            position: { type: 'integer', enum: [1, 2], description: '位置：1-首页顶部，2-首页中部' },
            positionText: { type: 'string', description: '位置文本' },
            startTime: { type: 'string', format: 'date-time', description: '开始时间' },
            endTime: { type: 'string', format: 'date-time', description: '结束时间' },
            status: { type: 'integer', enum: [0, 1], description: '状态：0-禁用，1-启用' },
            statusText: { type: 'string', description: '状态文本' },
            isActive: { type: 'boolean', description: '是否在有效期内' },
            createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
            updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
          }
        },
        CreateBannerRequest: {
          type: 'object',
          required: ['title', 'image', 'startTime', 'endTime'],
          properties: {
            title: { type: 'string', description: '轮播图标题' },
            image: { type: 'string', description: '轮播图图片URL' },
            linkUrl: { type: 'string', description: '跳转链接' },
            sortOrder: { type: 'integer', default: 0, description: '排序顺序' },
            position: { type: 'integer', enum: [1, 2], default: 1, description: '位置：1-首页顶部，2-首页中部' },
            startTime: { type: 'string', format: 'date-time', description: '开始时间' },
            endTime: { type: 'string', format: 'date-time', description: '结束时间' },
            status: { type: 'integer', enum: [0, 1], default: 1, description: '状态：0-禁用，1-启用' }
          }
        },
        UpdateBannerRequest: {
          type: 'object',
          properties: {
            title: { type: 'string', description: '轮播图标题' },
            image: { type: 'string', description: '轮播图图片URL' },
            linkUrl: { type: 'string', description: '跳转链接' },
            sortOrder: { type: 'integer', description: '排序顺序' },
            position: { type: 'integer', enum: [1, 2], description: '位置：1-首页顶部，2-首页中部' },
            startTime: { type: 'string', format: 'date-time', description: '开始时间' },
            endTime: { type: 'string', format: 'date-time', description: '结束时间' },
            status: { type: 'integer', enum: [0, 1], description: '状态：0-禁用，1-启用' }
          }
        },
        BannerListResponse: {
          type: 'object',
          properties: {
            banners: {
              type: 'array',
              items: { $ref: '#/components/schemas/Banner' },
              description: '轮播图列表'
            },
            total: { type: 'integer', description: '总数量' },
            page: { type: 'integer', description: '当前页码' },
            limit: { type: 'integer', description: '每页数量' },
            totalPages: { type: 'integer', description: '总页数' }
          }
        },
        BannerStatsResponse: {
          type: 'object',
          properties: {
            total: { type: 'integer', description: '总数量' },
            active: { type: 'integer', description: '启用数量' },
            inactive: { type: 'integer', description: '禁用数量' },
            expired: { type: 'integer', description: '已过期数量' },
            upcoming: { type: 'integer', description: '未开始数量' },
            byPosition: {
              type: 'object',
              additionalProperties: { type: 'integer' },
              description: '按位置统计'
            }
          }
        },
        ApiResponse: {
          type: 'object',
          properties: {
            code: { type: 'integer', description: '响应代码' },
            message: { type: 'string', description: '响应消息' },
            data: { type: 'object', description: '响应数据' },
            timestamp: { type: 'string', format: 'date-time', description: '时间戳' }
          }
        }
      }
    }
  },
  apis: [
    './src/routes/*.ts',
    './src/controllers/*.ts'
  ]
};

const swaggerSpec = swaggerJSDoc(options);

/**
 * 设置Swagger文档
 */
export const setupSwagger = (app: Express) => {
  app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
    explorer: true,
    customCss: '.swagger-ui .topbar { display: none }',
    customSiteTitle: '小米有品商城API文档'
  }));
  
  // Swagger JSON端点
  app.get('/api-docs.json', (req, res) => {
    res.setHeader('Content-Type', 'application/json');
    res.send(swaggerSpec);
  });
};